package com.advertisementsystem.Controller;

import com.advertisementsystem.Exception.ResponseResult;
import com.advertisementsystem.Servie.AdStatisticsService;
import com.advertisementsystem.utils.SecurityUtils;
import com.advertisementsystem.vo.StatisticsVO;
import lombok.extern.slf4j.Slf4j;
import org.apache.catalina.security.SecurityUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
@Slf4j
@RestController
@RequestMapping("/advertiser/statistics")
@PreAuthorize("hasRole('ADVERTISER')")
public class AdStatisticsController {

    @Autowired
    private AdStatisticsService statisticsService;

    @GetMapping
    public ResponseResult<StatisticsVO> getStatistics(
            @RequestParam @DateTimeFormat(pattern = "yyyy-MM-dd") Date startTime,
            @RequestParam @DateTimeFormat(pattern = "yyyy-MM-dd") Date endTime) {

        Long advertiserId = SecurityUtils.getUserId();
        log.info("advertiserId:{}", advertiserId);
        return ResponseResult.success(statisticsService.getStatistics(advertiserId, startTime, endTime));
    }
}